package com.retrofit.server.processor;

import com.okhttp.annotations.ApiGenerateService;
import com.okhttp.annotations.ApiResponseHandler;
import com.retrofit.server.processor.entity.ClassEntity;
import com.retrofit.server.processor.entity.GenericEntity;
import com.retrofit.server.processor.entity.HandlerTypeEnum;
import com.retrofit.server.processor.entity.MethodAnnotationEntity;
import com.retrofit.server.processor.entity.MethodCodeBlockEntity;
import com.retrofit.server.processor.entity.MethodEntity;
import com.retrofit.server.processor.entity.MethodReturnEntity;
import com.retrofit.server.processor.entity.ParameterEntity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.NoType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

/**
 * @author GYL
 * @dateTime 2020/5/9-9:38
 * @description 注解处理器
 */
public class AnnotationProcessor {

  static final String HANDLER_NAME = "BaseResponseHandler";
  static final String HANDLER_NAME2 = "BaseResponseBodyHandler";
  static final String KIND_VOID = "java.lang.Void";
  static final String KIND_RETROFIT2_CALLBACK = "retrofit2.Callback";
  public static final String HANDLER_PARAM_NAME="handler";
  
  
  private ClassEntity mClassEntity;
  
  private Elements mElementUtil;
  private Types typeUtils;
  private TypeElement mTypeElement;
  
  public ClassEntity getClassEntity(){
    return mClassEntity;
  }
  
  
  public AnnotationProcessor(Elements elementUtil, Types typeUtils, TypeElement typeElement) throws ProcessorException {
    this.mElementUtil = elementUtil;
    this.typeUtils = typeUtils;
    this.mTypeElement = typeElement;
    this.mClassEntity = new ClassEntity();
    analysisClass();
    //
    analysisAnnotation();
    //判断handler类型：
    HandlerTypeEnum handlerType = getHandlerType(mClassEntity.getApiGenerateService());
    if(handlerType==HandlerTypeEnum.baseResponseBodyHandler || handlerType== HandlerTypeEnum.baseResponsHandler){
      mClassEntity.setUseHandlerMehthod(true);
    }else {
      mClassEntity.setUseHandlerMehthod(false);
    }
    //判断当前类是否需要生成服务方法
    if(mClassEntity.isGenerateMehtod()){
      analysisMethods();
    }
  }
  
  /**
   * 解析注解 class
   */
  private void analysisAnnotation() {
    //获取Class注解value
    ApiGenerateService annntationClass = this.mTypeElement.getAnnotation(ApiGenerateService.class);
    //注解中有class 类型value时，直接取值容易报MirroredTypeException 异常
    try {
      Class<?> rhClass = annntationClass.value();
      TypeElement annotationClassTypeName = mElementUtil.getTypeElement(rhClass.getName());
      mClassEntity.setApiGenerateService(annotationClassTypeName.asType());
    } catch (MirroredTypeException mirtype) {
      mClassEntity.setApiGenerateService(mirtype.getTypeMirror());
    }
    try {
      boolean isGenerateMehtod = annntationClass.isGenerateMehtod();
      mClassEntity.setGenerateMehtod(isGenerateMehtod);
    }catch (Exception type){
      Print.log(type.getMessage());
      mClassEntity.setGenerateMehtod(true);
    }
  }
  
  /**
   * 解析当前Class信息
   */
  private void analysisClass() {
    //获取当前package , class name:
    PackageElement pkgElement = mElementUtil.getPackageOf(mTypeElement);
    String pkgName = pkgElement.isUnnamed() ? null : pkgElement.getQualifiedName().toString();
    String simpleName = mTypeElement.getSimpleName().toString();
    mClassEntity.setPackageName(pkgName);
    mClassEntity.setClazzName(simpleName);
    //
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm");
    String dateTime = sdf.format(new Date(System.currentTimeMillis()));
    StringBuilder javaDoc = new StringBuilder("\n")
                              .append("@author GYL\n")
                              .append("@dateTime ").append(dateTime).append("\n")
                              .append("@description  ").append(simpleName).append("接口封装实现类\n")
                              .append("@see ").append(simpleName).append("\n\n");
    mClassEntity.setJavadoc(javaDoc.toString());
    
    mClassEntity.setClassModifiers(new Modifier[]{Modifier.PUBLIC});
  }
  
  /**
   * 解析接口类中方法
   */
  private void analysisMethods() {
    //过滤掉父类方法
    List<ExecutableElement> executableElements = ElementFilter.methodsIn(mTypeElement.getEnclosedElements());
    if (StringUtils.isListNull(executableElements)) {
      Print.log(" 接口没有方法 ");
      return;
    }
    Print.log("typeParameterElement : ", executableElements.toString());
    //
    List<MethodEntity> methodList = new ArrayList<>();
    //解析出当前类所有接口方法：
    for (ExecutableElement etElement : executableElements) {
      Print.log("ExecutableElement:", etElement.toString());
      //1、解析方法注解：
      MethodAnnotationEntity methodAnnotationEntity = analysisMethodAnnotation(etElement);
      //方法注解集 @ApiResponseHandler
      if (!methodAnnotationEntity.isGenerate()) {
        //不生成此方法的实现
        continue;
      }
      /*
       * 处理方法返回类型，生成方法回调参数及代码块内容:<br>
       * 1、解析出返回值的类型：优先方法中注解类型，如果方法注解无值则使用类注解的值<br>
       * 2、根据注解泛型类型或返回值数据类型来添加回调参数泛型类型<br>
       * 3、添加新生成方法的返回值并添加泛型类型<br>
       * 4、添加调用代码块<br>
       * 5、如果为同步执行则需要返回Response<T>
       */
      MethodEntity methodEntity = new MethodEntity();
      methodEntity.setMethodAnnotationEntity(methodAnnotationEntity);
      //方法名称
      methodEntity.setMethodName(etElement.getSimpleName().toString());
      StringBuilder javaDocSb=new StringBuilder("");
      javaDocSb.append(String.format("{@link %s.%s#%s}$W\n", mClassEntity.getPackageName(), mClassEntity.getClazzName(), methodEntity.getMethodName()));
      //2、解析方法返回类型值
      MethodReturnEntity methodReturnEntity=analysisMethodReturn(etElement,methodAnnotationEntity.isSync());
      methodEntity.setReturnEntity(methodReturnEntity);
      //3、解析方法参数：
      List<ParameterEntity> parameterEntities = analysisMethodParameter(etElement, javaDocSb);
      //4、生成回调参数：
      //i.根据方法返回泛型类型和类泛型类型生成相应回调参数，优先方法中注解类型，如果方法注解无值则使用类注解的值
      ParameterEntity parameterEntity=generateHandlerParameter(methodAnnotationEntity,methodReturnEntity);
      if(parameterEntity!=null){
        parameterEntity.setHandler(true);
        parameterEntities.add(parameterEntity);
      }
      methodEntity.setParameterEntityList(parameterEntities);
      //5、添加方法代码块:根据注解是否同步进行生成代码：同步直接返回，异步则根据返回类型及回调参数进行生成
      generateCodeBlock(methodEntity,methodAnnotationEntity,methodReturnEntity);
      //6、添加代码注释
      methodEntity.setJavaDoc(javaDocSb.toString());
      methodList.add(methodEntity);
    }
    mClassEntity.setMethodEntityList(methodList);
  }
  
  /**
   * 解析方法中注解
   *
   * @param etElement 方法elmeent
   * @see ApiResponseHandler
   */
  private MethodAnnotationEntity analysisMethodAnnotation(ExecutableElement etElement) {
    MethodAnnotationEntity methodAnnotationEntity = new MethodAnnotationEntity();
    //默认使用class annontation handler
    methodAnnotationEntity.setApiResponseHandler(mClassEntity.getApiGenerateService());
    ApiResponseHandler annotationhandler = etElement.getAnnotation(ApiResponseHandler.class);
    //获取注解中值
    if (annotationhandler != null) {
      //handler
      try {
        Class<?> handlerClass = annotationhandler.handler();
        TypeMirror typeMirrorHandler = mElementUtil.getTypeElement(handlerClass.getName()).asType();
        methodAnnotationEntity.setApiResponseHandler(typeMirrorHandler);
      } catch (MirroredTypeException mirtype) {
        //判断是否为void
        if(mirtype.getTypeMirror()==null || KIND_VOID.equals(mirtype.getTypeMirror().toString())){
          methodAnnotationEntity.setApiResponseHandler(mClassEntity.getApiGenerateService());
        }else{
          methodAnnotationEntity.setApiResponseHandler(mirtype.getTypeMirror());
        }
      } catch (Exception e) {
        Print.log(e.getMessage());
        methodAnnotationEntity.setApiResponseHandler(mClassEntity.getApiGenerateService());
      }
      // genericity
      try {
        Class<?> genericityClass = annotationhandler.genericity();
        TypeMirror typeMirrorGenericity = mElementUtil.getTypeElement(genericityClass.getName()).asType();
        methodAnnotationEntity.setApiGenericity(typeMirrorGenericity);
      } catch (MirroredTypeException mirtype) {
        methodAnnotationEntity.setApiGenericity(mirtype.getTypeMirror());
      } catch (Exception e) {
        Print.log(e.getMessage());
      }
      // generate
      methodAnnotationEntity.setGenerate(annotationhandler.isGenerate());
      // sync
      methodAnnotationEntity.setSync(annotationhandler.isSync());
    }
    //获取类型
    HandlerTypeEnum handlerEnum=getHandlerType(methodAnnotationEntity.getApiResponseHandler());
    methodAnnotationEntity.setHandlerType(handlerEnum);
    return methodAnnotationEntity;
  }

  private HandlerTypeEnum getHandlerType(TypeMirror typeMirror){
    // 解析当前Handler 类型
    TypeElement handlerElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
    String handlerName=handlerElement.getSimpleName().toString();
    String handlerSuperName=null;
    if(handlerElement.getKind()== ElementKind.CLASS){
      boolean isStop=true;
      TypeElement superElement=  handlerElement;
      //取父类
      do {
        DeclaredType superclass = (DeclaredType) superElement.getSuperclass();
        superElement= (TypeElement) superclass.asElement();
        String superClassName=superElement.getSimpleName().toString();
        if(HANDLER_NAME.equals(superClassName) ||HANDLER_NAME2.equals(superClassName)){
          handlerSuperName=superClassName;
          isStop=false;
        }else if("Object".equals(superClassName)){
          isStop=false;
          handlerSuperName="";
        }
      }while (isStop);
    }else if(handlerElement.getKind()==ElementKind.INTERFACE){
      //callback interface
      handlerSuperName="";
    }
    if(HANDLER_NAME.equals(handlerName) || HANDLER_NAME.equals(handlerSuperName)){
      return HandlerTypeEnum.baseResponsHandler;
    }else if(HANDLER_NAME2.equals(handlerName) || HANDLER_NAME2.equals(handlerSuperName)){
      return HandlerTypeEnum.baseResponseBodyHandler;
    }else{
      return HandlerTypeEnum.callback;
    }
  }


  /**
   * 解析方法中参数
   * @param etElement
   * @param javaDocSb
   * @return
   */
  private List<ParameterEntity> analysisMethodParameter(ExecutableElement etElement,StringBuilder javaDocSb){
    List<? extends VariableElement> parameters = etElement.getParameters();
    List<ParameterEntity> parameterEntities=new ArrayList<>();
    for (VariableElement parameter : parameters) {
      String paramName=parameter.getSimpleName().toString();
      Print.log("method param name : ", paramName);
      ParameterEntity parameterEntity=new ParameterEntity();
      parameterEntity.setParamName(paramName);
      TypeMirror typeMirror = parameter.asType();
      parameterEntity.setClassTypeName(typeMirror);
      parameterEntity.setHandler(false);
      javaDocSb.append(String.format("@param %s \n",paramName));
      parameterEntities.add(parameterEntity);

    }
    return parameterEntities;
  }
  
  /**
   * 生成代码块
   * @param methodEntity
   * @param methodAnnotationEntity
   * @param methodReturnEntity
   */
  private void generateCodeBlock(MethodEntity methodEntity
    , MethodAnnotationEntity methodAnnotationEntity
    , MethodReturnEntity methodReturnEntity) {
    //同步方法
      if(methodAnnotationEntity.isSync()){
        MethodCodeBlockEntity codeBlockEntity=generateSyncCode(methodReturnEntity.getTmInterfaceReturn()
          ,methodEntity.getMethodName()
          ,methodEntity.getParameterNames()
          ,methodReturnEntity.isGenericT());
        methodEntity.setMethodBodyEntity(codeBlockEntity);
      }else{
        MethodCodeBlockEntity codeBlockEntity ;
        HandlerTypeEnum handlerEnum=methodAnnotationEntity.getHandlerType();
        //BaseResponeHandler 处理
        if(handlerEnum== HandlerTypeEnum.baseResponsHandler){
          codeBlockEntity=generateAsyncCode(methodReturnEntity.getTmInterfaceReturn()
            ,methodEntity.getMethodName(),methodEntity.getParameterNames(),true);
        }
        //BaseResponseBodyHandler
        else if(handlerEnum== HandlerTypeEnum.baseResponseBodyHandler){
          codeBlockEntity=generateAsyncCode(methodReturnEntity.getTmInterfaceReturn()
            ,methodEntity.getMethodName(),methodEntity.getParameterNames(),true);
        }
        //其他实现Call类
        else{
          codeBlockEntity=generateAsyncCode(methodReturnEntity.getTmInterfaceReturn()
            ,methodEntity.getMethodName(),methodEntity.getParameterNames(),false);
        }
        methodEntity.setMethodBodyEntity(codeBlockEntity);
      }
  }
  
  private MethodCodeBlockEntity generateAsyncCode(TypeMirror typeMirror,String methodName,String paramStr,boolean isCbWhat){
    MethodCodeBlockEntity methodBodyEntity=new MethodCodeBlockEntity();
    //添加代码块，生成 Call,Response<T>
    methodBodyEntity.add("int what=generateWhat()+handler.hashCode();\n");
      if(isCbWhat){
        methodBodyEntity.add("handler.onBeforeExecute(what);\n");
      }
      //添加代码块，生成 Call
      methodBodyEntity.add("$T call = innerCall().$N($N);$W\n",typeMirror,methodName,paramStr)
        .add("com.okhttp.retrofit.RetrofitClient.getInstance().addRetrofitCall(what,call);\n")
      //调用执行，传入回调参数
      .add("call.enqueue($N);$W\n", "handler")
      .add("return  what;\n");
    return methodBodyEntity;
  }
  
  
  private MethodCodeBlockEntity generateSyncCode(TypeMirror typeMirror,String methodName,String paramStr,boolean isT){
    MethodCodeBlockEntity methodBodyEntity=new MethodCodeBlockEntity();
    //添加代码块，生成 Call,Response<T>
    methodBodyEntity.add("$T call = innerCall().$N($N);$W\n", typeMirror,methodName,paramStr)
    .add("try {\n")
      .add(isT?"$W return (T) call.execute();$W":"$W return call.execute();$W")
      .add("\n} catch (Exception e) {")
      .add("\ncom.okhttp.utils.RetrofitLog.e(\"$N$N\",e.getMessage(),e); \n}finally{if(call!=null){call.cancel();}}\n",mClassEntity.getClazzName(),"Service")
      .add("return null;");
    return methodBodyEntity;
  }
  
  /**
   * 解析出返回值的类型 和 泛型<br>
   *   1、解析出返回值类型：生成调用方法</br>
   *   2、解析返回值泛型；Hander 参数使用</br>
   * @param etElement
   * @param isSync
   * @return MethodReturnEntity
   */
  private MethodReturnEntity analysisMethodReturn(ExecutableElement etElement,boolean isSync){

    //解析出返回值的类型：优先方法中注解类型，如果方法注解无值则使用类注解的值
    //接口方法的返回类型
    TypeMirror tmReturnType = etElement.getReturnType();
    //获取回调泛型类型
    if (!(tmReturnType instanceof NoType || tmReturnType.getKind() == TypeKind.VOID || tmReturnType.getKind() == TypeKind.NONE)) {
      Print.log("returnType : " + tmReturnType.toString());
      MethodReturnEntity methodReturnEntity = new MethodReturnEntity();
      //接口方法返回值
      methodReturnEntity.setTmInterfaceReturn(tmReturnType);
      methodReturnEntity.setSync(isSync);

      //取返回值中的泛型类型
      if (tmReturnType instanceof DeclaredType) {
        //取Call的返回类型中泛型
        GenericEntity genericEntity = new GenericEntity();
        TypeMirror returnGenerice;
        try {
          returnGenerice = ((DeclaredType) tmReturnType).getTypeArguments().get(0);
          genericEntity.addTypeMirror(returnGenerice);
          if (isSync) {
            //同步方法返回值：Response<T>
            TypeElement typeElement=mElementUtil.getTypeElement("retrofit2.Response");
            TypeMirror typeMirror=typeElement.asType();
            methodReturnEntity.setTmMethodReturn(typeMirror);
          } else {
            //返回一个int值
            TypeElement telementInt=mElementUtil.getTypeElement(Integer.class.getCanonicalName());
            methodReturnEntity.setTmMethodReturn(telementInt.asType());
            //非同步方法处理：
            //returnGenerice = ((DeclaredType) tmReturnType).getTypeArguments().get(0);
            //genericEntity.addTypeMirror(returnGenerice);
            TypeMirror childMirror = returnGenerice;
            GenericEntity childGeneric = genericEntity;
            //遍历获取返回中泛型类型
            do {
              List<? extends TypeMirror> mirrors = ((DeclaredType) childMirror).getTypeArguments();
              if (!StringUtils.isListNull(mirrors)) {
                //取第1个泛型数据
                GenericEntity child = new GenericEntity();
                TypeMirror typeMirror = mirrors.get(0);
                //child.setMirrorGeneric(typeMirror);
                child.addTypeMirror(typeMirror);
                childGeneric.setChildGeneric(child);
                //
                childGeneric = child;
                childMirror = typeMirror;
              } else {
                childMirror = null;
              }
            } while (childMirror != null);
          }
        } catch (Exception e) {
          if (isSync) {
            //异常时使用泛型 T
            genericEntity = new GenericEntity();
            genericEntity.addTypeName("T");
            methodReturnEntity.setGenericT(true);
          }
        }
        methodReturnEntity.setGenericEntity(genericEntity);
      }
      return methodReturnEntity;
    }else{
      Print.log("analysisMethodReturn 类型错误");
    }
    return null;
  }
  
  /**
   * 生成方法的回调参数
   * @param methodAnnotationEntity
   * @param methodReturnEntity
   * @return
   */
  private ParameterEntity generateHandlerParameter(MethodAnnotationEntity methodAnnotationEntity, MethodReturnEntity methodReturnEntity){
    //当前方法注解中handler为空时则使用Class responseHandler
    if(methodAnnotationEntity.getApiResponseHandler()==null){
      methodAnnotationEntity.setApiResponseHandler(mClassEntity.getApiGenerateService());
    }
    if(methodAnnotationEntity.isSync()){
      //同步方法直接返回
      return null;
    }else{
      TypeMirror typeMirrorHandler = methodAnnotationEntity.getApiResponseHandler();
      ParameterEntity entity = new ParameterEntity();
      entity.setClassTypeName(typeMirrorHandler);
      entity.setParamName("handler");
      HandlerTypeEnum handlerType = methodAnnotationEntity.getHandlerType();
      //注解中的数据泛型
      TypeMirror typeMirrorGeneric = methodAnnotationEntity.getApiGenericity();
      //String genericityname = typeMirrorGeneric == null ? KIND_VOID:typeMirrorGeneric.toString();
      if(handlerType==HandlerTypeEnum.baseResponsHandler){
        // baseResponseHandler
        GenericEntity genericEntity=generateBaseResponseHandler(typeMirrorHandler,typeMirrorGeneric,methodReturnEntity);
        entity.setGenericEntities(genericEntity);
      }else if(handlerType==HandlerTypeEnum.baseResponseBodyHandler){
        //baseResponseBodyHandler
        GenericEntity genericEntity=generateBaseResponseBodyHandler(typeMirrorHandler,typeMirrorGeneric,methodReturnEntity);
        entity.setGenericEntities(genericEntity);
      }else{
         //handlerType==HandlerTypeEnum.callback
        //其他Callback,直接使用接口返回类型的泛型
        GenericEntity returnGenericEntity = methodReturnEntity.getGenericEntity();
        entity.setGenericEntities(returnGenericEntity);
        return entity;
      }
      return entity;
    }
  }

  private GenericEntity generateBaseResponseBodyHandler(TypeMirror typeMirrorHandler,
      TypeMirror typeMirrorGeneric, MethodReturnEntity methodReturnEntity) {
    String genericityname = typeMirrorGeneric == null ? KIND_VOID : typeMirrorGeneric.toString();
    //泛型：根据Hander泛型个数添加：
    TypeElement element = (TypeElement) ((DeclaredType) typeMirrorHandler).asElement();
    List<? extends TypeParameterElement> typeParameters = element.getTypeParameters();
    // Handler类中泛型只有一个处理
    if (typeParameters != null && typeParameters.size() == 1) {
      GenericEntity genericEntity = new GenericEntity();
      Print.log(typeParameters.toString());
      //void
      if (!KIND_VOID.equals(genericityname)) {
        //使用注解中泛型类型
        genericEntity.addTypeMirror(typeMirrorGeneric);
      } else {
        genericEntity = new GenericEntity();
        genericEntity.addTypeName("T");
        methodReturnEntity.setGenericT(true);
      }
      return genericEntity;
    } else {
      //有多个泛型时处理：<E,T>
      GenericEntity genericEntity = new GenericEntity();
      if (!KIND_VOID.equals(genericityname)) {
        //第1个泛型：
        genericEntity.addTypeMirror(typeMirrorGeneric);
      } else {
        GenericEntity returnGeneric = methodReturnEntity.getGenericEntity();
        if (returnGeneric.getChildGeneric() != null) {
          //使用子泛型
          genericEntity.addTypeNames(returnGeneric.getChildGeneric().getTypeNameList());
          genericEntity.addTypeNames(returnGeneric.getTypeNameList());
        } else {
          genericEntity.addTypeName("T");
          genericEntity.addTypeNames(returnGeneric.getTypeNameList());
          methodReturnEntity.setGenericT(true);
        }
      }
      return genericEntity;
    }
  }

  private GenericEntity generateBaseResponseHandler(TypeMirror typeMirrorHandler,
      TypeMirror typeMirrorGeneric, MethodReturnEntity methodReturnEntity) {
    String genericityname = typeMirrorGeneric == null ? KIND_VOID : typeMirrorGeneric.toString();
    //泛型：根据Hander泛型个数添加：
    TypeElement element = (TypeElement) ((DeclaredType) typeMirrorHandler).asElement();
    List<? extends TypeParameterElement> typeParameters = element.getTypeParameters();
    // Handler类中泛型只有一个处理
    if (typeParameters != null && typeParameters.size() == 1) {
      GenericEntity genericEntity = new GenericEntity();
      Print.log(typeParameters.toString());
      //void
      if (!KIND_VOID.equals(genericityname)) {
        //使用注解中泛型类型
        genericEntity.addTypeMirror(typeMirrorGeneric);
      } else {
        //取接口返回值类型中的泛型：
        GenericEntity returnGeneric = methodReturnEntity.getGenericEntity();
        if (returnGeneric.getChildGeneric() != null) {
          //使用子泛型
          genericEntity.addTypeNames(returnGeneric.getChildGeneric().getTypeNameList());
        }
//        else {
//          genericEntity.addTypeNames(returnGeneric.getTypeNameList());
//        }
      }
      return genericEntity;
    } else {
      //有多个泛型时处理：<E,T>
      GenericEntity genericEntity = new GenericEntity();
      if (!KIND_VOID.equals(genericityname)) {
        //第1个泛型：
        genericEntity.addTypeMirror(typeMirrorGeneric);
        //第2个泛型：
        genericEntity.addTypeNames(methodReturnEntity.getGenericEntity().getTypeNameList());
      } else {
        GenericEntity returnGeneric = methodReturnEntity.getGenericEntity();
        if (returnGeneric.getChildGeneric() != null) {
          //使用子泛型
          genericEntity.addTypeNames(returnGeneric.getChildGeneric().getTypeNameList());
          genericEntity.addTypeNames(returnGeneric.getTypeNameList());
        } else {
          genericEntity.addTypeName("T");
          genericEntity.addTypeNames(returnGeneric.getTypeNameList());
          methodReturnEntity.setGenericT(true);
        }

      }
      return genericEntity;
    }
  }

  
  
}
